/*LICENSE_BEGIN
 * ******************************************************************
 * All original parts of SoccerScope2003 have copyright (2003) by
 * Team YowAI, represented by Mr. Shuhei Shiota
 * The Department of Creative Informatics
 * The Graduate School of Information Science and Technology
 * The University of Tokyo
 * 
 * Additional classes (except libraries in /lib) were developed by 
 * the Dainamite Robocup-Team of the DAI-Labor, TU-Berlin 
 * (www.dai-labor.de)
 * 
 * Extended/new classes/packages are:
 *   soccerscope.analyzer.LogfileAnalyzer
 *   soccerscope.daistatistic.*
 *   soccerscope.daistatistic.charts.*
 *   soccerscope.daistatistic.tools.*
 *   soccerscope.db.*
 *   soccerscope.dialog.*
 *   soccerscope.model.PlayerWorldModel
 *   soccerscope.model.SimplePlayer
 *   soccerscope.net.AgentConnector
 *   soccerscope.training.*
 *   soccerscope.training.conditions.*
 * ******************************************************************
LICENSE_END*/
package soccerscope.view;

import java.awt.event.ActionEvent;
import java.util.List;
import java.util.Vector;

import javax.swing.JComboBox;

import soccerscope.model.PlayMode;
import soccerscope.model.ScenePlayModeList;
import soccerscope.model.ScenePlayModeListItem;
import soccerscope.model.ScenePlayModeListObserver;
import soccerscope.model.SceneSet;
import soccerscope.model.WorldModel;

/**
 * Implements a GUI element to jump to scenes where the play mode has changed.
 * The SCenes are combined by play modes.
 */
public class PlayModeSortedJumper extends AbstractSceneJumper implements ScenePlayModeListObserver {

    private static final long serialVersionUID = -5531019185760203025L;

    /** Box where the available play modes could be choosed. */
    private JComboBox modes;
    
    /** Box where the scenes for the mode choosen in modes could be choosed. */
    private JComboBox scenesOfModes;
    
    /** Saves all play modes, which are shown in the box modes. */
    private List <PlayMode> modesInBox;

	private ScenePlayer scenePlayer;
    
    /**
     * Creates the GUI element.
     * @param scenePlayer the ScenePlayer to jumpt to a choosen scene
     */
    public PlayModeSortedJumper(ScenePlayer scenePlayer) {
        super(scenePlayer);
        this.scenePlayer = scenePlayer;
        SceneSet sceneSet = WorldModel.getSceneSet();
        ScenePlayModeList pml = sceneSet.getScenePlayModeList();
        
        modesInBox = pml.getOccuredModes();
        modes = new JComboBox(new Vector(modesInBox));
        add(modes);
        modes.addActionListener(this);
        pml.addObserver(this);
        chooseMode();
    }

    
    /**
     * Builds a ComboBox with the Scenes of the play mode, which is selected
     * in the box modes.
     */
    private void chooseMode() {
        if (scenesOfModes != null) {
            remove (scenesOfModes);
        }
        
        SceneSet sceneSet = WorldModel.getSceneSet();
        ScenePlayModeList pml = sceneSet.getScenePlayModeList();

        PlayMode selectedMode = (PlayMode) modes.getSelectedItem();
        scenesOfModes = new JComboBox(new Vector(pml.getScenesByPlayMode(selectedMode)));
        add(scenesOfModes);
        if (getParent() != null) {
            scenesOfModes.setVisible(true);
            getParent().invalidate();
            invalidate();
            scenesOfModes.invalidate();
            getParent().doLayout();
            doLayout();
            scenesOfModes.doLayout();
        }
    }
    
    /**
     * This method is called, when a the play mode has changed
     * @param item the scene number and the new play mode in one object
     */
    public void newScenePlayModeAdded(ScenePlayModeListItem item) {
        if (!modesInBox.contains(item.getMode())) {
            modesInBox.add(item.getMode());
            modes.addItem(item.getMode());
        } else if (modes.getSelectedItem().equals(item.getMode())) {
            scenesOfModes.addItem(item);
        }
    }

    /**
     * Thie method is called, when the whole list has changed, e.g. after loading
     *
     */
    public void allChanged() {
        SceneSet sceneSet = WorldModel.getSceneSet();
        ScenePlayModeList pml = sceneSet.getScenePlayModeList();

        remove(modes);
        modesInBox = pml.getOccuredModes();
        modes = new JComboBox(new Vector(modesInBox));
        add(modes);
        modes.addActionListener(this);
        modes.setVisible(true);
        chooseMode();
    }



    /**
     * will be called when the selection in the box modes has changed or if 
     * the jump button has be clicked. 
     */
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == modes) {
            chooseMode();
        } else {
            jumpToScene(((ScenePlayModeListItem) scenesOfModes.getSelectedItem()).getScene());
        }
        this.scenePlayer.repaint();
    }

}
